<script type="text/javascript" src="<?= $this->config->base_url(); ?>assets/js/jquery-1.4.3.min.js"></script>
<script type="text/javascript" src="<?= $this->config->base_url(); ?>assets/js/jquery-1.4.3.min.js"></script>
<script type="text/javascript" src="<?= $this->config->base_url(); ?>assets/js/jquery-ui.js"></script>
<link rel="stylesheet" href="<?= $this->config->base_url(); ?>assets/css/jquery-ui.css" type="text/css" charset="utf-8" />
<script type="text/javascript">
$(document).ready(function(){
	$('#received_date').datepicker({ dateFormat: 'yy-mm-dd' });
	$('#referance_date').datepicker({ dateFormat: 'yy-mm-dd' });
	
})

$(function() {
	     var scntDiv = $('#p_scents');
        var i = $('#p_scents tr').size() + 1;
        
        $('.addScnt').live('click', function() {
                $('<tr id="dd'+ i +'"> <td><input type="text" class="span8"  name="code[]" onchange="get_product1(this.value,rate'+ i +');"/></td> <td><input type="text"  class="span8" name="rate[]"   id="rate'+ i +'" onchange="count_all1(this.value, '+i+');"  /></td> <td><input type="text" class="span8" name="quantity[]" id="quantity'+ i +'" onblur="count_amount1(this.value, '+i+' );" /><input type="hidden" id="hid_qty'+ i +'" class="span8" ></td> <td><input type="text" class="span8" name="amount[]" readonly="readonly" id="amt'+ i +'" /><input type="hidden" id="hid_amt'+ i +'" class="span8" ></td> <td><input type="text" class="span8" name="discount[]" id="discount'+ i +'" onchange="count_distotal1(this.value, '+i+')"  /><input type="hidden" id="hid_disc'+ i +'" class="span8" ></td><td><select id="tax'+ i +'" onchange="count_total1(this.value, '+i+');"  class="span12"  name="tax[]" class="chzn-select"> <option value="" >Select</option><?php foreach($taxes as $tax) {?><option value="<?= $tax->amount?>"<?php if(isset($edit)) {if($edit['project_id'] == $tax->amount){echo 'selected = "selected"';}}else {echo set_select('project',$tax->id);}?> ><?= $tax->name?></option><?php }?></select><input type="hidden" id="hid_tax'+ i +'" class="span8" ></td><td><input type="text" class="span12" name="total[]" readonly="readonly"  id="total'+ i +'"/><input type="hidden" class="span12"  id="hid_total'+ i +'"/></td><td><input type="text"  class="span8"  name="balance[]"  onchange="count_balance1(this.value,'+i+')"  id="balance'+ i +'"/><input type="hidden" class="span12" value="0"  id="hid_balance'+ i +'"/></td><td><a href="#" class="addScnt">ADD</a>&nbsp;&nbsp;&nbsp;<a href="#"  id="'+i+'" class="remScnt">Remove</a></td></tr>').appendTo(scntDiv);
                i++;
                return false;
        });
        
        $('.remScnt').live('click', function() { 
		//alert(i)
		var Y= parseInt(this.id)
		var t='#'+'amt'+Y
		var tx='#'+'hid_tax'+Y
		var to='#'+'total'+Y
		var ds='#'+'hid_disc'+Y
		var h_excise = '#'+'hid_excise'+Y
		var excise = '#'+'excise'+Y
		var h_balance = '#'+'hid_balance'+Y
		var balance = '#'+'balance'+Y
		var c_amt= parseFloat($(t).val())
		var c_disc = parseFloat($(ds).val())
		var c_tax= parseFloat($(tx).val())
		var c_balance =  parseFloat($(balance).val())
		var c_excise =  parseFloat($(excise).val())
		var c_total	= parseFloat($(to).val())
		var f_amt= parseFloat($('#f_amt').val())
		var f_disc = parseFloat($('#f_disc').val())
		var f_excise= parseFloat($('#f_excise').val())
		var f_balance = parseFloat($('#f_balance').val())
		var f_tax= parseFloat($('#f_tax').val())
		var f_total	= parseFloat($('#f_total').val())
		if($(t).val()!=''){
			//$('#f_amt').val('123')
			$('#f_amt').val((f_amt - c_amt).toFixed(2))			
		}else{
			$('#f_amt').val((f_amt).toFixed(2))
			
		}
		if($(ds).val()!=''){
			$('#f_disc').val((f_disc - c_disc).toFixed(2))			
		}else{
			$('#f_amt').val((f_disc).toFixed(2))			
		}
		if($(excise).val()!=''){
			$('#f_excise').val((f_excise - c_excise).toFixed(2))			
		}else{
			$('#f_excise').val((f_excise).toFixed(2))			
		}
		if($(balance).val()!=''){
			$('#f_balance').val((f_balance - c_balance).toFixed(2))			
		}else{
			$('#f_balance').val((f_balance).toFixed(2))			
		}
		if($(tx).val()!=''){		
			$('#f_tax').val((f_tax - c_tax).toFixed(2))	
		}else{
			$('#f_amt').val((f_tax).toFixed(2))
			
		}
		if($(to).val()!=''){
				$('#f_total').val((f_total - c_total).toFixed(2))
			
		}else{
				$('#f_amt').val((f_total).toFixed(2))
		
			}
		
		    if( i > 2 ) {
                        
						$(this).parents('tr').remove();
                        i--;
                }
                return false;
        });
});


$(document).ready(function(e) {
jQuery('.edit_subtasks').click(function(event) {	
  
  event.preventDefault();
  jQuery.get(this.href, function(html) {
  jQuery(html).appendTo('body').modal(); 
  });
});
});
    


function get_product(code){
	$.post("<?= site_url('purchase/purchase/get_product')?>",{ code:code }, function(msg){
		if(msg!=1){
			$('#rate').val(msg)
			}else{
				$('#rate').val(""),
				alert('No Product Is Available!!')
				}
		
		})
	
	}
	
function get_product1(code,rate){
		var id="#"+$(rate).attr('id')
		$.post("<?= site_url('purchase/purchase/get_product')?>",{ code:code }, function(msg){
		if(msg!=1){
				$(id).val(msg);	

			}else{
				$(id).val(""),
				alert('No Product Is Available!!')
				}
		
		})
	
}	

function count_amount(qty,rate,code){
	if(qty==''){
		alert('Quantity Field Should Not be Empty!')
		if($('#hid_qty').val()==''){
			$('#quantity').focus()
			return false			
		}else{
			$('#quantity').val($('#hid_qty').val())	
		}		
		}else{
	
		if($('#f_amt').val()!=''){
		var n=parseInt(qty)
		var p=parseFloat(rate)
		var pr_n = parseFloat($('#hid_qty').val())
		var pr_disc = parseFloat($('#hid_disc').val())
		var pr_tax = parseFloat($('#hid_tax').val())
		var f_amt = parseFloat($('#f_amt').val())
		var f_tax = parseFloat($('#f_tax').val())
		var f_disc = parseFloat($('#f_disc').val())
		var pr_amt= p * pr_n
		var new_amt= p * n
		$('#amt').val(new_amt.toFixed(2))
		$('#f_amt').val((f_amt - pr_amt + new_amt).toFixed(2))
		$('#hid_qty').val(n)	
		var discount = parseFloat($('#discount').val())
		var tax = parseFloat($('#tax').val())
		var disc_per = (new_amt * discount)/100
		var temp_amt = new_amt - disc_per 
		var tax_per= (temp_amt * tax)/100
		var to_tax= temp_amt + tax_per
		if(f_tax==0 && f_disc==0){
			var temp_tax = tax_per
			var temp_tax = disc_per
		}else{
				var temp_tax = f_tax - pr_tax + tax_per 
				var temp_disc = f_disc - pr_disc + disc_per
		}
		$('#hid_tax').val(tax_per.toFixed(2))
		$('#hid_disc').val(disc_per.toFixed(2))
		$('#f_tax').val(temp_tax.toFixed(2))
		$('#f_disc').val(temp_disc.toFixed(2))
		$("#hid_amt").val(new_amt.toFixed(2))
		var f_total = parseFloat($('#f_total').val())
		var total = parseFloat($('#total').val())
		var	n_total= to_tax
		var fn_total= f_total - total + n_total
		$('#total').val(n_total.toFixed(2))
		$('#hid_total').val(n_total.toFixed(2))
		$('#f_total').val(fn_total.toFixed(2))
		
	}else{
		n=parseInt(qty)
		p=parseFloat(rate)
		$('#hid_qty').val(n)
		$('#amt').val((n*p).toFixed(2))	
		$('#f_amt').val((n*p).toFixed(2))
		$("#hid_amt").val((n*p).toFixed(2))
	}
	
	}
	
	}


var counter1=0
	function count_total(tax, dis, amt){
		if(amt == ''){
		return false	
		}
		if(tax != ''){
		if(dis=='')
			{
			var tax_per,disc_per;
				
			p=parseFloat(amt),
			t=parseFloat(tax),
			tax_per= (p*t)/100,
			to_tax= p+tax_per,
			disc_per= 0,
			final_amt=to_tax ,
			counter1=counter1+1
			if(counter1 >1){
		
				var f_tax = parseFloat($("#f_tax").val())
				var f_total = parseFloat($("#f_total").val())
				var total = parseFloat($("#total").val())
				var h_tax = parseFloat($("#hid_tax").val())
		
				$("#hid_tax").val((tax_per).toFixed(2))
			
				$("#f_tax").val((f_tax-h_tax+tax_per).toFixed(2))
				$("#total").val(final_amt.toFixed(2))
				$("#hid_total").val(final_amt.toFixed(2))
				$("#f_total").val((f_total - total + final_amt).toFixed(2))
				
			}else{
				$('#total').val(final_amt.toFixed(2))
				$("#hid_total").val(final_amt.toFixed(2))
				
				$('#f_tax').val(tax_per.toFixed(2))
				$('#f_disc').val(disc_per.toFixed(2))
				$('#f_total').val(final_amt.toFixed(2))
				$("#hid_tax").val(tax_per.toFixed(2))
				$("#hid_disc").val(disc_per.toFixed(2))
				
			}
				
			}
			else if( dis !=''){
					var tax_per,disc_per
			p=parseFloat(amt)
			t=parseFloat(tax)
			d=parseFloat(dis)
			disc_per = (p * d)/100
			var temp_amt = p - disc_per 
			tax_per= (temp_amt * t)/100
			to_tax= temp_amt + tax_per
			final_amt=to_tax
			
			counter1=counter1+1
			if(counter1 >1){
		
				var f_tax=parseFloat($("#f_tax").val())	
				var f_total=parseFloat($("#f_total").val())
				var total=parseFloat($("#total").val())
				var h_tax=parseFloat($("#hid_tax").val())
		
				$("#hid_tax").val(tax_per.toFixed(2))
				$("#f_tax").val((f_tax-h_tax+tax_per).toFixed(2))
				
				
				//alert(final_amt)
				$("#total").val(final_amt.toFixed(2))
				$("#hid_total").val(final_amt.toFixed(2))
				$("#f_total").val((f_total - total + final_amt).toFixed(2))	
			}else{
				$('#total').val(final_amt.toFixed(2))
				$('#f_tax').val(tax_per.toFixed(2))
				$('#f_disc').val(disc_per.toFixed(2))
				$('#f_total').val(final_amt.toFixed(2))
				$("#hid_total").val(final_amt.toFixed(2))
				$("#hid_tax").val(tax_per.toFixed(2))
				$("#hid_disc").val(disc_per.toFixed(2))
				
					
			}
			}
			
		else{
			//alert("i am here")
			var tax_per,disc_per
			p = parseFloat(amt)
			t = parseFloat(tax)
			d = parseFloat(dis)
			disc_per = (p * d)/100
			var temp_amt = p - disc_per 
			tax_per= (temp_amt * t)/100
			to_tax= temp_amt + tax_per
			final_amt=to_tax
			counter1=counter1+1
			if(counter1 >1){
				var f_tax = parseFloat($("#f_tax").val())	
				
				var f_total = parseFloat($("#f_total").val())
				var total=parseFloat($("#total").val())
				var h_tax=parseFloat($("#hid_tax").val())
			
				$("#hid_tax").val(tax_per.toFixed(2))
				$("#f_tax").val((f_tax-h_tax+tax_per).toFixed(2))
				
				//alert(final_amt)
				$("#total").val(final_amt.toFixed(2))
				$("#hid_total").val(final_amt.toFixed(2))
				$("#f_total").val((f_total - total + final_amt).toFixed(2))	
			}else{
				$('#total').val(final_amt.toFixed(2))
				$('#f_tax').val(tax_per.toFixed(2))
				$('#f_disc').val(disc_per.toFixed(2))
				$('#f_total').val(final_amt.toFixed(2))
				$("#hid_total").val(final_amt.toFixed(2))
				$("#hid_tax").val(tax_per.toFixed(2))
				$("#hid_disc").val(disc_per.toFixed(2))		
				
				
			}		
		}
		}else{
		alert('Select a Tax!')	
		
		}

	
	}




	function count_distotal(dis){
		if($('#total').val()!=''){
		var d=parseFloat(dis)
		var f_total = parseFloat($('#f_total').val())	
		var c_total = parseFloat($('#total').val())	
		var c_amt = parseFloat($('#amt').val())
		var tax = parseFloat($('#tax').val())
			
		if(dis==''){
					var disc_per = 0	
		}else{
		var disc_per = (c_amt * d)/100				
		}
		var temp_amt = c_amt - disc_per 
 		var h_disc = parseFloat($('#hid_disc').val())	
		var f_disc = parseFloat($('#f_disc').val())
		var h_tax = parseFloat($('#hid_tax').val())	
		var f_tax = parseFloat($('#f_tax').val())
		var tax_per = (temp_amt * tax)/100
		//$('#hid_amt').val(c_amt)
		$('#f_disc').val((f_disc - h_disc + disc_per ).toFixed(2))
		$('#hid_disc').val(disc_per.toFixed(2))
		$('#f_tax').val((f_tax - h_tax + tax_per ).toFixed(2))
		$('#hid_tax').val(tax_per.toFixed(2))
		$('#total').val((c_total + h_disc - disc_per - h_tax + tax_per).toFixed(2))
		$("#hid_total").val((c_total + h_disc - disc_per - h_tax + tax_per).toFixed(2))
		$('#f_total').val((f_total + h_disc - disc_per - h_tax + tax_per).toFixed(2))
		$('#hid_excise').val(excise)
		}
	}




/*function count_amount1(qty,rate,x){
	var f_amt=parseFloat($('#f_amt').val())
	var id="#"+$(rate).attr('id')
	var r,
	r=$(id).val();
	n=parseInt(qty),
	p=parseFloat(r)
	var t='#'+'amt'+x
	$(t).val(n*p)
	var flag=n*p
	$('#f_amt').val(f_amt+flag)
	
	}*/

var counter=0
function count_total1(tax, x){
	var a='#'+'amt'+x
	if($(a).val()==''){
		return false	
	}
	if(tax != ''){
	var f_total = parseFloat($('#f_total').val())
	var f_tax = parseFloat($('#f_tax').val())
	var f_excise = parseFloat($('#f_excise').val())
	var f_disc=parseFloat($('#f_disc').val())
	var tax_per,disc_per;
	var a='#'+'amt'+x
	var d='#'+'discount'+x
	var total='#'+'total'+x
	var hid_tax='#'+'hid_tax'+x
	var hid_disc='#'+'hid_disc'+x
	var hid_excise ='#'+'hid_excise'+x
	var hid_total='#'+'hid_total'+x
	var excise = '#'+'excise'+x
	if($(excise).val()!= ''){
		ex = parseFloat($(excise).val())
		
	}else{
		ex = 0
	}
	p=parseFloat($(a).val())
	if($(d).val()!=''){
			d=parseFloat($(d).val())
			disc_per= (p*d)/100
			$(hid_disc).val(disc_per)

	}else{
		disc_per=0;
		$(hid_disc).val(disc_per)

	}
	var temp_amt = p - disc_per + ex
	t=parseFloat(tax)
	tax_per= (temp_amt * t)/100
	to_tax= temp_amt + tax_per
	final_amt=to_tax
	counter=counter+1
	if($(hid_tax).val()!=''){
		var h_tax=parseFloat($(hid_tax).val())
	//	alert(f_tax - h_tax + tax_per)
		$("#f_tax").val((f_tax - h_tax + tax_per).toFixed(2))
		$("#f_excise").val((f_excise + ex).toFixed(2))
		//alert(final_amt)
		var h_total=parseFloat($(total).val())
		$(hid_tax).val(tax_per.toFixed(2))
		$(hid_excise).val(ex.toFixed(2))
		$(total).val(final_amt.toFixed(2))
		$(hid_total).val(final_amt.toFixed(2))
		
		$("#f_total").val((f_total-h_total+final_amt).toFixed(2))	
		
	}else{
			$("#f_total").val((f_total+final_amt).toFixed(2))
			$(total).val(final_amt.toFixed(2))
			$(hid_total).val(final_amt.toFixed(2))
			$(hid_excise).val((ex).toFixed(2))
			$("#f_excise").val((f_excise + ex).toFixed(2))
			$("#f_tax").val((f_tax + tax_per).toFixed(2))
			$("#f_disc").val((f_disc + disc_per).toFixed(2))
			$(hid_tax).val(tax_per.toFixed(2))
			$(hid_disc).val(disc_per.toFixed(2))
		}
		}else{
			alert("Select A Tax!!")
		}
		
	}


	function count_amount1(qty,x){
		var q='#'+'hid_qty'+x
		var c='#'+'code'+x
		var code = $(c).val()
	if(qty==''){
		var quantity='#'+'quantity'+x
		alert('quantity Filed Should not be Empty!!')
		if($(q).val()==''){
		$(quantity).focus()
		return false
		}else{
		$(quantity).val($(q).val())
				
		}
	}else{
		
		var total='#'+'total'+x
		
	if($(total).val()!=''){
			//alert('ok')
			var excise ='#'+'excise'+x
			var r='#'+'rate'+x
			var p_a ='#'+'hid_amt'+x
			var p_d='#'+'hid_disc'+x
			var p_excise ='#'+'hid_excise'+x
			var excise ='#'+'excise'+x
			
			var p_t ='#'+'hid_tax'+x
			var r='#'+'rate'+x
			var c_amt='#'+'amt'+x
			var c_total='#'+'total'+x
			var discount='#'+'discount'+x
			var hid_total='#'+'hid_total'+x
			var tax='#'+'tax'+x
		if($(excise).val()==''){
			var ex = 0 
		}else{
			var ex = parseFloat($(excise).val())
		}
		var pr_n = parseFloat($(q).val())
		var n=parseInt(qty)
		var p=parseFloat($(r).val())
		
		var pr_disc = parseFloat($(p_d).val())
		var pr_tax = parseFloat($(p_t).val())
		var f_amt = parseFloat($('#f_amt').val())
		var f_tax = parseFloat($('#f_tax').val())
		var f_disc = parseFloat($('#f_disc').val())
		//alert(temp_tax)
	//	alert(temp_disc)
	
		var pr_amt= p * pr_n
		var new_amt= p * n
		$(c_amt).val(new_amt.toFixed(2))
		$(p_a).val(new_amt.toFixed(2))
		$('#f_amt').val((f_amt - pr_amt + new_amt).toFixed(2))
		$(q).val(n)	
		var discount = parseFloat($(discount).val())
		var tax = parseFloat($(tax).val())
		var disc_per = (new_amt * discount)/100
		var temp_amt = new_amt - disc_per + ex
		var tax_per = (temp_amt * tax)/100
		var to_tax= temp_amt + tax_per
		//var disc_per= (new_amt*discount)/100
		if(f_tax==0 && f_disc==0){
			var temp_tax = tax_per
			var temp_tax = disc_per
		}else{
				var temp_tax = f_tax - pr_tax + tax_per 
				var temp_disc = f_disc - pr_disc + disc_per
		}
		
		$(p_t).val(tax_per.toFixed(2))
		$(p_d).val(disc_per.toFixed(2))
		$('#f_tax').val(temp_tax.toFixed(2))
		$('#f_disc').val(temp_disc.toFixed(2))
		var f_total = parseFloat($('#f_total').val())
		var total = parseFloat($(total).val())
		var	n_total= to_tax
		var fn_total= f_total - total + n_total
		//alert(f_total)
		//alert(total)
		//alert(n_total)
		//alert(fn_total)
		$(c_total).val(n_total.toFixed(2))
		$(hid_total).val(n_total.toFixed(2))
		$('#f_total').val(fn_total.toFixed(2))
		
	}else{
	var f_amt=parseFloat($('#f_amt').val())
	var r='#'+'rate'+x
	var hid_amt='#'+'hid_amt'+x
	r=$(r).val();
	n=parseInt(qty),
	p=parseFloat(r)
	var t='#'+'amt'+x
	var hid_qty='#'+'hid_qty'+x
	$(hid_qty).val(qty)
	$(t).val((n*p).toFixed(2))
	var flag=n*p
	$(hid_amt).val((n*p).toFixed(2))
	$('#f_amt').val((f_amt+flag).toFixed(2))
	}
	
	
		
	}

	
}



	function count_distotal1(dis,x){
		var total='#'+'total'+x
		if($(total).val()!=''){
			alert('hello')
		var d=parseFloat(dis)
		var p_d = '#'+'hid_disc'+x
		var p_t ='#'+'hid_tax'+x
		var excise = '#'+'excise'+x
		var p_excise = '#'+'excise'+x 
		var t ='#'+'tax'+x
		var hid_total='#'+'hid_total'+x
		var p_total='#'+'total'+x
		var amt='#'+'amt'+x
		var f_total = parseFloat($('#f_total').val())	
		var c_total=parseFloat($(p_total).val())	
		var c_amt=parseFloat($(amt).val())	
		if(dis==''){
			var disc_per = 0	
		}else{
			
		var disc_per = (c_amt * d)/100		
		}
		if($(excise).val() == ''){
		var ex = 0	
		}else{
			
		var ex = parseFloat($(excise).val())		
		}
	
		
		var temp_amt = c_amt - disc_per + ex
		var h_disc = parseFloat($(p_d).val())	
		var f_disc = parseFloat($('#f_disc').val())
		var h_excise = parseFloat($(p_excise).val())	
		var f_excise = parseFloat($('#f_excise').val())
			
	//	alert(h_disc)
	//	alert(f_disc)
		var tax =  parseFloat($(t).val())
		var h_tax = parseFloat($(p_t).val())	
		var f_tax = parseFloat($('#f_tax').val())
		var  tax_per = (temp_amt * tax)/100
		//
		//alert(f_disc - h_disc + disc_per)
		$('#f_disc').val((f_disc - h_disc + disc_per ).toFixed(2))
		$('#f_tax').val((f_tax - h_tax + tax_per ).toFixed(2))
		$('#f_excise').val((f_excise - h_excise + ex).toFixed(2))
		$(p_excise).val(ex.toFixed(2))
		$(p_d).val(disc_per.toFixed(2))
		$(p_t).val(tax_per.toFixed(2))
		$(total).val((c_total + h_disc - disc_per - h_tax + tax_per ).toFixed(2))
		$(hid_total).val((c_total + h_disc - disc_per - h_tax + tax_per).toFixed(2))
		$('#f_total').val((f_total + h_disc - disc_per - h_tax + tax_per).toFixed(2))
		}
	}
	
	
	
	
	function count_excise1(excise,x){
		var total='#'+'total'+x
		if($(total).val()!=''){
		var dis = '#'+'discount'+x
		var p_d = '#'+'hid_disc'+x
		var p_t ='#'+'hid_tax'+x
		var p_excise = '#'+'hid_excise'+x 
		var t ='#'+'tax'+x
		var hid_total='#'+'hid_total'+x
		var p_total='#'+'total'+x
		var amt='#'+'amt'+x
		var f_total = parseFloat($('#f_total').val())	
		var c_total=parseFloat($(p_total).val())	
		var c_amt=parseFloat($(amt).val())	
		if($(dis).val()== ''){
			var disc_per = 0	
		}else{
			d = parseFloat($(dis).val())
			var disc_per = (c_amt * d)/100		
		}
		if(excise == ''){
			var ex = 0	
		}else{
			var ex = parseFloat(excise)		
		}
	
		//alert(c_amt)
		//alert(disc_per)
		//alert(ex)
		
		var temp_amt = c_amt - disc_per + ex
		var h_disc = parseFloat($(p_d).val())	
		var f_disc = parseFloat($('#f_disc').val())
		var h_excise = parseFloat($(p_excise).val())	
		var f_excise = parseFloat($('#f_excise').val())
			
	//	alert(h_disc)
	//	alert(f_disc)
		//alert(temp_amt)
		var tax =  parseFloat($(t).val())
		var h_tax = parseFloat($(p_t).val())	
		var f_tax = parseFloat($('#f_tax').val())
		var  tax_per = (temp_amt * tax)/100
		//alert(tax_per)
		//
		//alert(f_disc - h_disc + disc_per)
		$('#f_disc').val((f_disc - h_disc + disc_per ).toFixed(2))
		$('#f_tax').val((f_tax - h_tax + tax_per ).toFixed(2))
		$('#f_excise').val((f_excise - h_excise + ex).toFixed(2))
		$(p_excise).val(ex.toFixed(2))
		$(p_d).val(disc_per.toFixed(2))
		$(p_t).val(tax_per.toFixed(2))
		$(total).val((c_total + h_disc - disc_per - h_tax + tax_per - h_excise + ex  ).toFixed(2))
		$(hid_total).val((c_total + h_disc - disc_per - h_tax + tax_per - h_excise + ex).toFixed(2))
		$('#f_total').val((f_total + h_disc - disc_per - h_tax + tax_per - h_excise + ex).toFixed(2))
		
		}
	}
	
	
	
	
	
	
	
	function count_all(rate){
		if($('#quantity').val()!='' && $('#total').val()!='' ){
			var r=parseFloat(rate)
			var qty=parseFloat($('#quantity').val())
			$('#amt').val((r*qty).toFixed(2))
			var final_amt=parseFloat($('#f_amt').val())
			var pre_amt=parseFloat($('#hid_amt').val())
			var amt = parseFloat((r*qty).toFixed(2))
			$('#f_amt').val((final_amt- pre_amt+ amt).toFixed(2))			
			var final_tax=parseFloat($('#f_tax').val())
			var final_disc=parseFloat($('#f_disc').val())
			var pre_tax=parseFloat($('#hid_tax').val())
			var pre_disc=parseFloat($('#hid_disc').val())			
			//alert($('#total').val())
			p=parseFloat(amt)
			t=parseFloat($('#tax').val())
			d=parseFloat($('#discount').val())
			disc_per =  (p*d)/100
			var temp_amt = p - disc_per 
			tax_per= (temp_amt * t)/100
			var temp_tax = final_tax - pre_tax + tax_per
			//alert(temp_tax)
			var to_tax = temp_amt + tax_per
			var temp_disc = final_disc - pre_disc + disc_per
			$('#f_tax').val(temp_tax.toFixed(2))
			$('#f_disc').val(temp_disc.toFixed(2))
			//alert(temp_disc)
			var temp_total = to_tax  
			$('#hid_tax').val(tax_per.toFixed(2))
			$('#hid_disc').val(disc_per.toFixed(2))
			$('#total').val(temp_total.toFixed(2))
			var pre_total = parseFloat($('#hid_total').val())
			var pre_final =parseFloat($('#f_total').val())
			//alert(pre_final)
			//alert(pre_total)
			//alert(temp_total)
			//alert(pre_final - pre_total + temp_total)
			$('#f_total').val((pre_final - pre_total + temp_total).toFixed(2))
			$('#hid_total').val(temp_total.toFixed(2))
		}else if($('#quantity').val()!=''){
			var r=parseFloat(rate)
			var qty=parseFloat($('#quantity').val())
			$('#amt').val((r*qty).toFixed(2))
			$('#hid_amt').val((r*qty).toFixed(2))
			$('#f_amt').val((r*qty).toFixed(2))
					
	
		}

	}
	
	function count_unit(rate){
		if($('#quantity').val()!='' && $('#total').val()!='' ){
			var r=parseFloat(rate)
			var qty=parseFloat($('#quantity').val())
			$('#amt').val((r*qty).toFixed(2))
			var final_amt=parseFloat($('#f_amt').val())
			var pre_amt=parseFloat($('#hid_amt').val())
			var amt = parseFloat((r*qty).toFixed(2))
			$('#f_amt').val((final_amt- pre_amt+ amt).toFixed(2))			
			var final_tax=parseFloat($('#f_tax').val())
			var final_disc=parseFloat($('#f_disc').val())
			var pre_tax=parseFloat($('#hid_tax').val())
			var pre_disc=parseFloat($('#hid_disc').val())			
			//alert($('#total').val())
			p=parseFloat(amt)
			t=parseFloat($('#tax').val())
			d=parseFloat($('#discount').val())
			disc_per =  (p*d)/100
			var temp_amt = p - disc_per 
			tax_per= (temp_amt * t)/100
			var temp_tax = final_tax - pre_tax + tax_per
			//alert(temp_tax)
			var to_tax = temp_amt + tax_per
			var temp_disc = final_disc - pre_disc + disc_per
			$('#f_tax').val(temp_tax.toFixed(2))
			$('#f_disc').val(temp_disc.toFixed(2))
			//alert(temp_disc)
			var temp_total = to_tax  
			$('#hid_tax').val(tax_per.toFixed(2))
			$('#hid_disc').val(disc_per.toFixed(2))
			$('#total').val(temp_total.toFixed(2))
			var pre_total = parseFloat($('#hid_total').val())
			var pre_final =parseFloat($('#f_total').val())
			//alert(pre_final)
			//alert(pre_total)
			//alert(temp_total)
			//alert(pre_final - pre_total + temp_total)
			$('#f_total').val((pre_final - pre_total + temp_total).toFixed(2))
			$('#hid_total').val(temp_total.toFixed(2))
		}else if($('#quantity').val()!=''){
			var r=parseFloat(rate)
			var qty=parseFloat($('#quantity').val())
			$('#balance_amt').val((r*qty).toFixed(2))
			$('#hid_amt').val((r*qty).toFixed(2))
			$('#f_amt').val((r*qty).toFixed(2))
					
	
		}

	}
	
	
	
	
	
	function count_all1(rate,x){
				var quantity = '#'+'quantity'+x
				var total = '#'+'total'+x
				var p_amt = '#'+'amt'+x
				var hid_amt = '#'+'hid_amt'+x
				var hid_tax = '#'+'hid_tax'+x
				var hid_disc = '#'+'hid_disc'+x
				var tax = '#'+'tax'+x
				var discount = '#'+'discount'+x
				var hid_total = '#'+'hid_total'+x
				
		if($(quantity).val()!='' && $(total).val()!='' ){
			var r=parseFloat(rate)
			var qty=parseFloat($(quantity).val())
			var final_amt=parseFloat($('#f_amt').val())
			var pre_amt=parseFloat($(hid_amt).val())
			var amt = parseFloat(r*qty)
			//$(p_amt).val(amt)
			//alert(final_amt- pre_amt+ amt)
			$('#f_amt').val(final_amt- pre_amt+ amt)			
			var final_tax=parseFloat($('#f_tax').val())
			var final_disc=parseFloat($('#f_disc').val())
			var pre_tax=parseFloat($(hid_tax).val())
			var pre_disc=parseFloat($(hid_disc).val())			
			//alert($('#total').val())
			p=parseFloat(amt)
			t=parseFloat($(tax).val())
			d=parseFloat($(discount).val())
			disc_per = (p*d)/100
			var temp_disc= final_disc - pre_disc + disc_per
			var temp_amt = p - disc_per 
			tax_per= (temp_amt * t )/100
			var temp_tax = final_tax - pre_tax + tax_per
			//alert(temp_tax)
			var to_tax= temp_amt + tax_per
			$('#f_tax').val(temp_tax.toFixed(2))
			$('#f_disc').val(temp_disc.toFixed(2))
			$(p_amt).val(amt.toFixed(2))
			$(hid_amt).val(amt.toFixed(2))
			//alert(temp_disc)
			var temp_total = to_tax
			$(hid_tax).val(tax_per.toFixed(2))
			$(hid_disc).val(disc_per.toFixed(2))
			$(total).val(temp_total.toFixed(2))
			var pre_total = parseFloat($(hid_total).val())
			var pre_final =parseFloat($('#f_total').val())
			//alert(pre_final)
			//alert(pre_total)
			//alert(temp_total)
			//alert(pre_final - pre_total + temp_total)
			$('#f_total').val((pre_final - pre_total + temp_total).toFixed(2))
			$(hid_total).val(temp_total.toFixed(2))
		}else if($(quantity).val()!=''){
			var r=parseFloat(rate)
			var qty=parseFloat($(quantity).val())
			var final_amt=parseFloat($('#f_amt').val())
			var pre_amt=parseFloat($(hid_amt).val())
			var amt = parseFloat(r*qty)	
			//$(p_amt).val(r*qty)
			$(p_amt).val(amt.toFixed(2))
			$(hid_amt).val(amt.toFixed(2))
			$('#f_amt').val((final_amt- pre_amt+ amt).toFixed(2))
			
		}

	}
	
	function count_balance(balance){
		var f_balance = $('#f_balance').val();
		if(f_balance == '0'){
				if(balance == ''){
					var c_balance = parseFloat(balance)
					$('#hid_balance').val(c_balance.toFixed(2))
					$('#f_balance').val(c_balance.toFixed(2))
			
				}else{
					
					var c_balance = parseFloat(balance)
					$('#hid_balance').val(c_balance.toFixed(2))
					$('#f_balance').val(c_balance.toFixed(2))
				}
			}else{
				if(balance == ''){
					var hid_balance = parseFloat($('#hid_balance').val())
					var final_balance = parseFloat(f_balance)
					var  c_balance   = parseFloat(balance)
					var temp_balance = parseFloat(final_balance - hid_balance)  
					$('#hid_balance').val('0')
					$('#f_balance').val(temp_balance)
				}else{
					var hid_balance = parseFloat($('#hid_balance').val())
					var final_balance = parseFloat(f_balance)
					var  c_balance   = parseFloat(balance)
					var c_balance = parseFloat(balance)
					var temp_balance = parseFloat(final_balance - hid_balance + c_balance)  
					$('#hid_balance').val(c_balance.toFixed(2))
					$('#f_balance').val(c_balance.toFixed(2))
				}
			}	
	}
	
	
	
		function count_balance1(balance,x){
		var f_balance = $('#f_balance').val();
		if(f_balance == '0'){
				if(balance == ''){
					var c_balance = parseFloat(balance)
					var hid_balance = '#'+'hid_balance'+x
					$(hid_balance).val(c_balance.toFixed(2))
					$('#f_balance').val(c_balance.toFixed(2))
			
				}else{
					var c_balance = parseFloat(balance)
					var hid_balance = '#'+'hid_balance'+x
					$(hid_balance).val(c_balance.toFixed(2))
					$('#f_balance').val(c_balance.toFixed(2))
				}
			}else{
				if(balance == ''){
					var hid_balance = '#'+'hid_balance'+x
					var h_balance = parseFloat($(hid_balance).val())
					var final_balance = parseFloat(f_balance)
					var  c_balance   = parseFloat(balance)
					var temp_balance = parseFloat(final_balance - h_balance)  
					$(hid_balance).val('0')
					$('#f_balance').val(temp_balance.toFixed(2))
				}else{
					var hid_balance = '#'+'hid_balance'+x
					var h_balance = parseFloat($(hid_balance).val())
					var final_balance = parseFloat(f_balance)
					var  c_balance   = parseFloat(balance)
					var temp_balance = parseFloat(final_balance - h_balance + c_balance)  
					//alert(temp_balance)
					$(hid_balance).val(c_balance.toFixed(2))
					$('#f_balance').val(temp_balance.toFixed(2))
				}
			}	
	}
	
	

	

	var rount_counter = 0; 
function set_total(round_off){
	rount_counter = rount_counter + 1 
	var rd = "0"+"."+round_off
		var round = parseFloat(rd)
	if(rount_counter > 1){	
		var h_round = parseFloat($('#hid_round').val());
		var total = parseFloat($('#f_total').val());	
		$('#hid_round').val(round.toFixed(2))
		$('#f_total').val((total - h_round + round).toFixed(2))
		
	}else{
		var total = parseFloat($('#f_total').val())
		$('#hid_round').val(round.toFixed(2))
		$('#f_total').val((total + round).toFixed(2))

		}	
	}

</script>




<?php echo form_open("purchase/purchase/add_purchase");?>

<!--<div id="infoMessage"><?php echo $message;?></div>-->
<table align="center">
<tr><td><br>SUPPLIER</td><td>
            <?php 
	 	$bl[0] = "";
	   		foreach($suppliers as $supplier){
				$bl[$supplier->id] = $supplier->name;
			}
			echo form_dropdown('id_supplier', $bl, '', 'class="chzn-select span12" data-placeholder="Choose a Supplier" style="width:220px;"');  ?>
	
<a href="<?=site_url('people/supplier/create_supplier/st')?>" ><i class="icon-edit"></i></a> 
</td></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr>
<td>REFERANCE NO:</td><td><?php echo form_input($referance_no);?></td>
</tr>
<td>REFERANCE DATE:</td><td><?php echo form_input($referance_date);?></td>
</tr>  
<tr>      
<td>RECEIVED DATE</td><td><?php echo form_input($received_date);?></td>
</tr>
<tr><td>TYPE</td><td> <label class="checkbox">
    <input type="radio" name="type" value="0" checked="checked">:Cash
    </label>
    <label class="checkbox">
<input type="radio" name="type" value="2" checked="checked">:Credit
</label>
</td>
</tr>
<tr><td><br>Branch</td>
<td>
     <select name="id_branch" class="span12 chzn-select">
    <option value="0">-- Warehouse -- </option>
    <?php foreach ($branches as $branch) {?>
    <option value="<?=$branch->id?>" <?php 
	if(isset($edit)) {
	if($edit['user_id'] == $branch->id)
	{
		echo 'selected = "selected"';
		}
	}
	else {
	echo set_select('id_branch',$branch->id);
	}
	?> ><?= $branch->name;?></option >
    <?php } ?>
    </select></td></tr>


</table>

<hr>
<hr>
<table width="100%" class="table table-bordered" >

  <tr class="success">
              <td><b>Item Code</b></td>
              <td><b>Rate</b></td>
              <td><b>Quantity</b></td>
              <td><b>Amount</b></td>
              <td><b>Discount(%)</b></td>
              <td><b>Tax(%)</b></td>
              <td><b>Total</b></td>
              <td><b>Balance</b></td>
			  <td><b>Options</b></td>
             </tr>
  <tbody id="p_scents">           
  <tr><label for="p_scnts">
    <td><input type="text" class="span8" name='code[]' onblur="get_product(this.value);" /></td>
    <td><input type="text" class="span8"  id="rate" onchange="count_all(this.value);"  name='rate[]'/><input type="hidden"   id="hid_rate"/></td>
    <td><input type="text" class="span8" name="quantity[]" id="quantity" onblur="count_amount(this.value, $('#rate').val())" /><input type="hidden" id="hid_qty" class="span8" ></td>
    <td><input type="text" class="span8" name="amount[]"  readonly="readonly" id="amt" /><input type="hidden" id="hid_amt" class="span8" ></td>
    <td><input type="text" class="span8" name="discount[]" id="discount" onchange="count_distotal(this.value)" /><input type="hidden" id="hid_disc" class="span8" ></td>
    <td><select class="span12"  name="tax[]" id="tax" class="chzn-select" onchange="count_total(this.value, $('#discount').val(), $('#amt').val(), $('#excise').val() )">
     <option value="" >Select</option>
    <?php foreach($taxes as $tax) {?>
    <option value="<?= $tax->amount?>" 
    <?php 
	if(isset($edit)) {
		if($edit['project_id'] == $project->id)
		{
			echo 'selected = "selected"';
		}
	}
	else {
	echo set_select('project',$tax->amount);
	}
	?> 
     ><?= $tax->name?></option>
    <?php }?>
    </select>
    <input type="hidden" id="hid_tax" class="span8" >
    </td>
  	<td><input type="text" class="span12" name="total[]" readonly="readonly" id="total" /> <input type="hidden" id="hid_total" class="span8" ></td>
<td><input type="text" class="span8"  id="balance"    name='balance[]' onchange="count_balance(this.value)"  /><input type="hidden" class="span8" value="0"  id="hid_balance" /></td>
<td><a href="#" class="addScnt">ADD</a></td>
  </label></tr>
  </tbody>
</table>
<div class="row-fluid">
<div class="span5">
<table >
<tr>
<td><input type="checkbox" onclick="$('.trans').show()" name="transport" id="transport">:Transport</td><td>&nbsp;&nbsp;&nbsp;</td>
</tr>
<tr class="trans" style="display:none">
<td><b>Transport Name</b></td><td><input type="taxt" name="trans_name" id="trans_name" class="span12"></td>
</tr>
<tr class="trans" style="display:none">
<td><b>Amount</b></td><td><input type="text" class="span8" name="trans_amt"></td>
</tr>

</table>
</div>


<div class="span7">
<table>
<tr>
<td><b>Amount</b></td><td><input type="text" class="span5" name="total_amount" id="f_amt" ></td>
</tr>
<tr>
<td><b>Total Discount</b></td><td><input type="text" name="total_discount" class="span5" id="f_disc" ></td>
</tr>
<tr>
<td><b>Total Tax</b></td><td><input type="text" name="total_tax" class="span5" id="f_tax"></td>
</tr>
<tr>
<td><b>Total Balance</b></td><td><input type="text"  value="0" name="total_balance" class="span5" id="f_balance" ></td>
</tr>
<tr>
<td><b>Round Off</b></td><td><input type="text" onchange="set_total();"  value="0."  readonly="readonly"  class="span2"><input type="text" onchange="set_total(this.value);" value="00" maxlength="2" name="round_off" class="span2"><input type="hidden" id="hid_round"></td>
</tr>
<tr>
<td colspan="2">--------------------------------------------------------------------------</td>
<tr>
<td><b>Total Amount</b></td><td><input type="text" name="total_price" class="span5" id="f_total"></td>
</tr>

</tr>

</table>
</div>
</div>
<p align="right"><?php echo form_submit('submit', 'Add Purchase', 'class="btn btn-success"');?></p>
<?php echo form_close();?>
